package com.dfbz.bigdata.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dfbz.bigdata.common.utils.R;
import com.dfbz.bigdata.modules.system.doman.CommonDic;

import com.dfbz.bigdata.modules.system.doman.CommonGoods;
import com.dfbz.bigdata.modules.system.dto.CommonDicDto;
import com.dfbz.bigdata.modules.system.service.CommonDicService;

import com.dfbz.bigdata.modules.system.service.CommonGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther:东方标准
 * @Date:2022/9/29-09-29-16:56
 * @Decsription:com.dfbz.bigdata.modules.system.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/dic")
@Slf4j
public class CommonDicController {

    @Autowired
    private CommonDicService commonDicService;

    @Autowired
    private CommonGoodsService commonGoodsService;

//    @GetMapping("/list")
//    public R<Page> getdiclist(String dicname,Long currentPage,Long pagesizes){
//
//        Page page =new Page(currentPage,pagesizes);
//
//        LambdaQueryWrapper<CommonDic> lqw=new LambdaQueryWrapper<>();
//        lqw.like(!dicname.isEmpty(),CommonDic::getDcName,dicname);
//        commonDicService.page(page,lqw);
//        return R.success(page);
//    }
    @GetMapping("/list")
    public R<List<CommonDicDto>> getdiclist(String dicname, int currentPage, int pagesizes){
        List<CommonDicDto> list = commonDicService.getlist(dicname, currentPage, pagesizes);

        return R.success(list);
    }
    @GetMapping("/total")
    public R<Integer> getDicTotal(){

        return R.success(commonDicService.getTotal());
    }
    @GetMapping("/get/index/{type}")
    public R<List<CommonDicDto>> gettypeindexlist(@PathVariable Long type){
        LambdaQueryWrapper<CommonDic> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CommonDic::getDcType,type);
        List<CommonDic> list = commonDicService.list(lqw);
        List<CommonDicDto> listdto = new ArrayList<>();
        for (CommonDic dic:list) {
            Long dcId = dic.getDcId();
            LambdaQueryWrapper<CommonDic> lqwdic=new LambdaQueryWrapper<>();
            lqwdic.eq(CommonDic::getDcType,dcId);
            List<CommonDic> list2 = commonDicService.list(lqwdic);
            List dicchildren=new ArrayList();
            for (CommonDic dic1:list2){
                dicchildren.add(dic1.getDcId());
            }


            LambdaQueryWrapper<CommonGoods> lqw1=new LambdaQueryWrapper<>();
            lqw1.in(CommonGoods::getCategoryId,dicchildren);
            List<CommonGoods> list1 = commonGoodsService.list(lqw1);
            List<CommonGoods> goodsthreelist=new ArrayList<>();
            CommonDicDto dicdto=new CommonDicDto();
            if (list1.size()!=0){
                if (list1.size()<3){
                    goodsthreelist=list1;
                }else {
                    goodsthreelist.add(0,list1.get(0));
                    goodsthreelist.add(1,list1.get(1));
                    goodsthreelist.add(2,list1.get(2));



                }
                dicdto.setDcId(dic.getDcId());
                dicdto.setDcName(dic.getDcName());
                dicdto.setChildren(goodsthreelist);
                listdto.add(dicdto);
            }


        }
        return R.success(listdto);
    }
    @GetMapping("/get/{type}")
    public R<List<CommonDic>> gettypelist(@PathVariable Long type){
        LambdaQueryWrapper<CommonDic> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CommonDic::getDcType,type);
        List<CommonDic> list = commonDicService.list(lqw);


        return R.success(list);
    }

    @GetMapping("/{id}")
    public R<CommonDic> getByIdDic(@PathVariable Long id){
        CommonDic commonDic=commonDicService.getById(id);

        return R.success(commonDic);
    }
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id){
        boolean b = commonDicService.removeById(id);
        if (b){
            return R.success("删除成功^_^!!!!!");
        }
        else return  R.error("系统繁忙-_-!!");
    }
    @PutMapping("update")
    public R<String> updateDic(@RequestBody CommonDic commonDic){

        commonDic.setUpdateTime(new Date());
        boolean b = commonDicService.updateById(commonDic);
        if (b){
            return R.success("更新成功^_^!!!!!");
        }
        return R.error("服务器繁忙-_-!!");

    }
    @PostMapping("insert")
    public R<String> insert(@RequestBody CommonDic commonDic){
        log.info("-----------------{}",commonDic);
        commonDic.setCreateTime(new Date());
        commonDic.setUpdateTime(new Date());
        boolean b = commonDicService.save(commonDic);
        if (b){
            return R.success("新增成功^_^!!!!!");
        }
        return R.success("服务器繁忙-_-!!");

    }
}
